define([
    "app"
], function (app) {
    app.controller("manageTeacherController", [
        "$scope",
        "manageTeacherDataService",
        "$uibModal",
        function ($scope, manageTeacherDataService, $uibModal) {
            $scope.state = "loading";

            manageTeacherDataService
                .getAllTeacher()
                .then(
                    function success(resp) {
                        $scope.state = "finished";

                        // 所有的导师
                        $scope.allTeachers = resp.data;

                        // 打开添加导师的对话框
                        $scope.addTeacher = function () {
                            $uibModal.open({
                                backdrop: "static",
                                templateUrl: "add-teacher-dialog.html",
                                size: "sm",
                                controller: "addTeacherDialogController",
                                resolve: {
                                    allTeachers: function () {
                                        return $scope.allTeachers;
                                    }
                                }
                            })
                        };

                        // 打开修改导师的对话框
                        $scope.updateTeacher = function (teacher) {
                            $uibModal.open({
                                backdrop: "static",
                                templateUrl: "update-teacher-dialog.html",
                                size: "sm",
                                controller: "updateTeacherDialogController",
                                resolve: {
                                    thisTeacher: function () {
                                        return teacher;
                                    }
                                }
                            });
                        };

                        // 打开删除导师的对话框
                        $scope.deleteTeacher = function (teacher) {
                            $uibModal.open({
                                backdrop: "static",
                                templateUrl: "delete-teacher-dialog.html",
                                size: "sm",
                                controller: "deleteTeacherDialogController",
                                resolve: {
                                    allTeachers: function () {
                                        return $scope.allTeachers;

                                    },
                                    thisTeacher: function () {
                                        return teacher;
                                    }
                                }
                            });
                        };

                    }, function error(resp) {
                        $scope.state = "error";
                    });
        }]);

    // 添加导师对话框
    app.controller("addTeacherDialogController", [
        "$scope",
        "manageTeacherDataService",
        "allTeachers",
        "$uibModalInstance",
        function ($scope, manageTeacherDataService, allTeachers, $uibModalInstance) {

            $scope.tutorTitles = [
                "教授",
                "副教授",
                "讲师",
                "助理教授"
            ];

            $scope.newTeacher = {
                tutorName: "",
                tutorTitle: "",
                phoneNumber: "",
                email: ""
            };


            $scope.dismiss = function () {
                $uibModalInstance.dismiss();
            };

            // 标示请求是否结束
            $scope.isDone = true;
            $scope.submit = function () {
                $scope.isDone = false;
                manageTeacherDataService
                    .addTeacher($scope.newTeacher)
                    .then(
                        function success(resp) {
                            allTeachers.push($scope.newTeacher);

                            $uibModalInstance.dismiss();

                            $scope.isDone = true;
                        },
                        function error(resp) {
                            alert("好像出了点问题");

                            $scope.isDone = true;
                        }
                    )
            };
        }
    ]);

    // 修改导师对话框
    app.controller("updateTeacherDialogController", [
        "$scope",
        "manageTeacherDataService",
        "$uibModalInstance",
        "thisTeacher",
        function ($scope, manageTeacherDataService, $uibModalInstance, thisTeacher) {

            $scope.tutorTitles = [
                "教授",
                "副教授",
                "讲师",
                "助理教授"
            ];

            $scope.tempTeacher = {};
            // 深拷贝这个对象进行暂存
            angular.copy(thisTeacher, $scope.tempTeacher);

            $scope.dismiss = function () {
                $uibModalInstance.dismiss();
            };

            // 请求是否结束
            $scope.isDone = true;
            $scope.submit = function () {

                $scope.isDone = false;
                manageTeacherDataService
                    .updateTeacher($scope.tempTeacher)
                    .then(
                        function success(resp) {
                            angular.copy($scope.tempTeacher, thisTeacher);

                            $uibModalInstance.dismiss();

                            $scope.isDone = true;
                        },
                        function error(resp) {
                            alert("好像出了点问题");

                            $scope.isDone = true;
                        }
                    )
            };


        }
    ]);

    app.controller("deleteTeacherDialogController", [
        "$scope",
        "manageTeacherDataService",
        "thisTeacher",
        "allTeachers",
        "$uibModalInstance",
        function ($scope, manageTeacherDataService, thisTeacher, allTeachers, $uibModalInstance) {
            $scope.dismiss = function () {
                $uibModalInstance.dismiss();
            };

            // 请求是否结束
            $scope.isDone = true;
            $scope.submit = function () {
                $scope.isDone = false;
                manageTeacherDataService
                    .deleteTeacher(thisTeacher)
                    .then(
                        function success(resp) {
                            allTeachers.splice(allTeachers.lastIndexOf(thisTeacher), 1);

                            $uibModalInstance.dismiss();

                            $scope.isDone = true;
                        },
                        function error(resp) {
                            alert("好像出了点问题");

                            $scope.isDone = true;
                        }
                    );
            };
        }
    ])
});